package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.service.MarketService
import com.zxy.supplier_system.server.service.MarketService.MarketDetail.AdministrativeRegionResponse
import com.zxy.supplier_system.server.utils.IntIdAndName
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*

@RequestMapping("markets")
@RestController
class MarketController(
    private val marketService: MarketService
) {

    data class QueryMarketsRequest(
        val name: String?,
    )

    @HasAnyAuthority(Authority.卖场_查询)
    @GetMapping(params = ["page"])
    fun queryMarkets(pageable: Pageable, queryMarketsRequest: QueryMarketsRequest): Page<QueryMarketsResponse> {
        return this.marketService.queryMarkets(pageable, queryMarketsRequest)
    }

    data class QueryMarketsResponse(
        val name: String,
        val id: Int,
        val saleManager:IntIdAndName?,
        val excludeBrands:List<IntIdAndName>,
        val shoppingGuides: List<IntIdAndName>,
        val goodsAmount:Int,
        val province: AdministrativeRegionResponse?,
        val city: AdministrativeRegionResponse?,
        val district: AdministrativeRegionResponse?,
        val longitude:Double?,
        val latitude:Double?,
        val address:String?,
        val addressName:String?,
    )

    data class UpdateMarketRequest(
        val name: String,
        val saleManagerId:Int?,
        val excludeBrandIds:List<Int> = emptyList(),
        val shoppingGuideIds:List<Int> = emptyList(),
        val districtCode: String?,
        val longitude:Double?,
        val latitude:Double?,
        val address:String?,
        val addressName:String?,
    )

    @HasAnyAuthority(Authority.卖场_修改)
    @PutMapping("{id}")
    fun updateMarket(@PathVariable id: Int, @RequestBody updateMarketRequest: UpdateMarketRequest) {
        this.marketService.updateMarket(id, updateMarketRequest)
    }

    @HasAnyAuthority(Authority.卖场_新增)
    @PostMapping
    fun createMarket(@RequestBody updateMarketRequest: UpdateMarketRequest): Int {
        return this.marketService.createMarket(updateMarketRequest).id
    }

    @DeleteMapping("{id}")
    fun deleteMarket(@PathVariable id: Int) {
        this.marketService.deleteMarket(id)
    }

    @GetMapping
    fun getAllMarkets(): List<IntIdAndName> {
        return this.marketService.getAllMarkets()
    }

    @HasAnyAuthority(Authority.卖场_查询)
    @GetMapping("{id}")
    fun getMarketDetail(@PathVariable id: Int):MarketService.MarketDetail{
        return this.marketService.getMarketDetail(id)
    }

    @GetMapping(params = ["projection=summary"])
    @HasAnyAuthority(Authority.卖场_查询)
    fun queryMarketSummary(keyword:String?,pageable: Pageable):Page<IntIdAndName>{
        return this.marketService.queryMarketSummary(keyword,pageable)
    }

    @GetMapping(params = ["onlyICanSee=true"])
    fun getAllMarketsOnlyICanSee():List<IntIdAndName>{
        return this.marketService.getAllMarketsOnlyICanSee()
    }

}